<?php
/**
 * Created by PhpStorm.
 * User: andy
 * Date: 16/4/9
 * Time: 12:22
 */
class Model_Lottery extends PhalApi_Model_NotORM {

    function gettotal() {
        return $this->getORM()
            ->where('islottery',1)
            ->count("id");
    }
    function getAllList() {
        return $this->getORM()->fetchAll();
    }
    function gettotalByToday() {
        return $this->getORM()
            ->where('islottery',1)
            ->where("DATE(lotterytime)",DATE("Y-m-d"))
            ->count("id");
    }

    function gettotalUser($openid) {
        return $this->getORM()
            ->where('islottery',1)
            ->where("openid",$openid)
            ->count("id");
    }

    function gettotalUserByToday($openid) {
        return $this->getORM()
            ->where('islottery',1)
            ->where("openid",$openid)
            ->where("DATE(lotterytime)",DATE("Y-m-d"))
//            ->where("DATE(lotterytime) = to_days(now())")
            ->count("id");
    }
    function insertUser($openid,$time,$ip,$scores) {
        return $this->getORM()
            ->insert(array(
                "openid"=>$openid,
                "lotterytime"=>$time,
                "ip"=>$ip,
                "scores"=>$scores,
                "islottery"=>0
            ));

    }

    function setLottery($id){
        return $this->getORM()
            ->where('id',$id)
            ->update(array(
                'islottery'=>1
            ));
    }

    function top($num) {
        return $this->getORM()
            ->select('openid,scores')
            ->limit($num)
            ->group('openid')
            ->order("scores desc,lotterytime desc")
            ->fetchAll();
    }


    protected function getTableName($id) {
        return 'wxvk_Lottery_History';
    }


}